package service.impl;

import action.showordSetting;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.intellij.notification.*;
import com.intellij.openapi.project.Project;
import compontent.StorageComponent;
import service.AlertService;

import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


public class AlertServiceImpl implements AlertService {

    private long time;

    private final NotificationGroup NOTIFICATION_GROUP =
            new NotificationGroup("Groovy DSL errors", NotificationDisplayType.TOOL_WINDOW, true);


    /**
     * 显示弹窗
     *
     * @param project     当前项目上下文
     * @param timeMinutes 时间（分钟）
     */
    @Override
    public void showAlertDialog(Project project, int timeMinutes,int word,int page) {
        if (time == 0) {
            time = System.currentTimeMillis();
        }
        Notification notification = NOTIFICATION_GROUP.createNotification("该记单词啦"+Integer.parseInt(StorageComponent.getValue("wordIndex")), "",
                NotificationType.INFORMATION, NotificationListener.URL_OPENING_LISTENER);

        notification.setContent(getSubtitle(page,word));
        notification.notify(project);
        /*强制弹框*/
//        Random random = new Random();
//        AlertDialog alertDialog = new AlertDialog(project,
//                Constant.Infor.TITLE,
//                new MessageBuilder().setBody(Constant.Infor.HARM[random.nextInt(Constant.Infor.HARM.length)]).setVariables(timeMinutes).build());
//        alertDialog.show();
    }

    /**
     * 单词显示  一天n个单词
     * @param page   页数
     * @param word   单词数
     * @return
     */
    public String getSubtitle(int page, int word) {
        String res = null;
        try {
            LinkedList<String> readLines = FileUtil.readLines(Objects.requireNonNull(showordSetting.class.getClassLoader().getResource("word/CET4.json")), StandardCharsets.UTF_8, new LinkedList<>());
            int subIndex = 0;
            if (0!=page){
                subIndex = page * word - 1;
            }
            if (page * word > readLines.size()){
                subIndex = readLines.size() - word;
            }

            List<String> list = readLines.subList(subIndex, subIndex+word);

            JSONObject jsonObject = JSONUtil.parseObj(list.get(Integer.parseInt(StorageComponent.getValue("wordIndex"))));
            String headWord = jsonObject.getStr("headWord");
            JSONArray jsonArray = jsonObject.getJSONObject("content").getJSONObject("word").getJSONObject("content").getJSONArray("trans");
            String phone = jsonObject.getJSONObject("content").getJSONObject("word").getJSONObject("content").getStr("phone");

            List<String> tranCn = jsonArray.stream().map(obj -> {
                JSONObject entries = JSONUtil.parseObj(obj);
                return entries.getStr("pos")+" "+entries.getStr("tranCn")+"\n";
            }).collect(Collectors.toList());

            res = String.format("%s\n%s [%s]",headWord, tranCn.toString().replace("[","").replace("]",""),phone);

            StorageComponent.save("wordIndex", Integer.parseInt(StorageComponent.getValue("wordIndex"))+1+"");
            if (Integer.parseInt(StorageComponent.getValue("wordIndex"))>=word){
                StorageComponent.save("wordIndex","0");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    public static void main(String[] args) {

        System.err.println(1+1+"");
    }
}
